<?php

namespace app\api\traits;

use think\Collection;
use think\db\BaseQuery as Query;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;
use think\Model;

trait Common
{
	/**
	 * 获取where条件
	 * @param array $data 参数数组
	 * @param array $fields 要处理的字段
	 * @return array
	 */
	public static function getSelfWhereCommon(Array $data , $fields = []) {
		$keys = array_keys($data);
		if($fields) {
			//去掉data里面fields没指定的字段
			$keys = array_intersect($keys , $fields);
		}
		$where = [];
		foreach($keys as $key) {
			$val = $data[$key] ?? '';
			if($val === '') continue; //过滤空值
			switch ($key) {
				default:
					$where[] = [$key, '=', $val];
			}
		}
		return $where;
	}

	/**
	 * @param array $where
	 * @param null $isCacheDb
	 * @return array|mixed|Model|null
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws ModelNotFoundException
	 */
	public static function existCommon($where=[], $field='id', $isCacheDb=null) {
		return self::field($field)->where($where)->find();
	}

	/**
	 * 获取一条数据
	 * @param $where
	 * @param string $field
	 * @param false $order
     * @param $cache
	 * @return self|Common|array|mixed|Model|null
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws ModelNotFoundException
	 */
	public static function getLineCommon($where,$field='*',$order=false, $cache=null) {
		$query = self::where($where)->order($order)->field($field);
        if(!is_null($cache)) {
            $query->cache($cache);
        }
        return $query->find();
	}

	/**
	 * 根据主键获取一条数据
	 * @param $pk
	 * @param string $field
     * @param $cache
	 * @return array|mixed|Model|null|self
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws ModelNotFoundException
	 */
	public static function getLineByPkCommon($pk, $field='*', $cache=null) {
		$query = self::field($field);
        if(!is_null($cache)) {
            $query->cache($cache);
        }
        return $query->find($pk);
	}

	/**
	 * 获取单个值(单条)
	 * @param $where
	 * @param $field
     * @param $cache
	 * @return mixed
	 */
	public static function getValueCommon($where,$field, $cache=null) {
		$query = self::where($where);
        if(!is_null($cache)) {
            $query->cache($cache);
        }
        return $query->value($field);
	}

	/**
	 * 获取全部数据
	 * @param array $where
	 * @param string $field
	 * @param string $order
     * @param null $cache
	 * @return array|Collection|self[]
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws ModelNotFoundException
	 */
	public static function getAllCommon($where=[], $field='*', $order='', $cache=null) {
		$query = self::field($field)->where($where)->order($order);
        if(!is_null($cache)) {
            $query->cache($cache);
        }
        return $query->select();
	}

	/**
	 * 获取列表,指定列.数组形式返回.全部返回
	 * @param [] $where
	 * @param string $field
	 * @param false $order
     * @param $cache
	 * @return array
	 */
	public static function getColumnAllCommon($where = [] , $field='*' , $order=false, $cache=null) {
        $key = '';
        $fieldInfo = '';
        if(is_array($field)) {
            $fieldInfo = $field[0];
            $key = $field[1];
        } else {
            $fieldInfo = $field;
        }
		$query = self::where($where)->order($order);
        if(!is_null($cache)) {
            $query->cache($cache);
        }
        return $query->column($fieldInfo, $key);
	}

    /**
     * group情况下获取列表带分页
     * @param $group
     * @param $where
     * @param $field
     * @param $page
     * @param $limit
     * @param $order
     * @return false|\ListWithCountStruct|void
     */
    public static function getGroupListWithCountCommon($group, $where=[], $field='*', $page=1, $limit=10, $order=false) {
        $total = self::where($where)->group($group)->count('*');
        if($total === false) return false;
        if($total == 0) return self::listWithCountCommon([], 0);
        $list  = self::where($where)->page($page, $limit)->order($order)->field($field)->group($group)->select();
        if($list === false) return false;

        return self::listWithCountCommon($list, $total);
    }

	/**
     * 获取列表带分页
	 * @param [] $where
	 * @param string $field
	 * @param int $page
	 * @param int $limit
	 * @param false $order
     * @param null $cache
	 * @return false|\ListWithCountStruct
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws ModelNotFoundException
	 */
	public static function getListWithCountCommon($where=[] , $field='*' , $page=1 , $limit=10 , $order=false, $cache=null) {
		$total = self::getCountCommon($where, $cache);
		if($total === false) return false;
		if($total == 0) return self::listWithCountCommon([], 0);

		$list  = self::getListCommon($where , $field , $page , $limit , $order, $cache);
		if($list === false) return false;

		return self::listWithCountCommon($list , $total);
	}

	/**
	 * 获取列表
	 * @param [] $where
	 * @param string $field
	 * @param int $page
	 * @param int $limit
	 * @param false $order
     * @param null $cache
	 * @return array|Collection
	 * @throws DataNotFoundException
	 * @throws DbException
	 * @throws ModelNotFoundException
	 */
	public static function getListCommon($where = [] , $field='*' , $page=1 , $limit=10 , $order=false, $cache=null) {
        if($where instanceof Query) {
            $query = $where;
        } else {
            $query = self::where($where);
        }
		$query = $query->page($page, $limit)->order($order)->field($field);
        if(!is_null($cache)) {
            $query->cache($cache);
        }
        return $query->select();
	}

	/**
	 * 获取列表.指定列.数组形式返回,支持分页
	 * @param [] $where
	 * @param string $field
	 * @param int $page
	 * @param int $count
	 * @param false $order
	 * @return array
	 */
	public static function getColumnListCommon($where = [] , $field='*' , $page=1 , $count=10 , $order=false) {
        $key = '';
        $fieldInfo = '';
        if(is_array($field)) {
            $fieldInfo = $field[0];
            $key = $field[1];
        } else {
            $fieldInfo = $field;
        }
		return self::where($where)->page($page , $count)->order($order)->column($fieldInfo, $key);
	}

	/**
	 * 获取记录数
	 * @param $where
     * @param null $cache
	 * @return int
	 */
	public static function getCountCommon($where=[], $cache=null) {
        if($where instanceof Query) {
            $query = $where;
        } else {
            $query = self::where($where);
        }
        if(!is_null($cache)) {
            $query->cache($cache);
        }
        return $query->count();
	}

	/**
	 * 删除
	 * @param $where
	 * @return bool
	 */
	public static function delCommon($where) {
		if(!$where) {
			return false;
		}
        return self::where($where)->delete();
	}

	/**
	 * 删除,按主键
	 * @param $pk
	 * @return bool
	 */
	public static function delByPkCommon($pk) {
		return self::destroy($pk);
	}

	/**
	 * 修改
	 * @param $data
	 * @param array $where
     * @param $limit
	 * @return Common
	 */
	public static function updateCommon($data, $where=[], $limit=null) {
        $query = self::where($where);
        if(!is_null($limit)) {
            $query->limit($limit);
        }
		return $query->update($data);
	}

	/**
	 * 自增
	 * @param $where
	 * @param $field
	 * @param int $step
     * @param $data
	 * @return mixed
	 */
	public static function incCommon($where , $field , $step=1, $data=[]) {
		return self::where($where)->inc($field, $step)->update($data);
	}

    /**
     * 自增,多字段
     * @param $where
     * @param $data
     * @return mixed
     */
    public static function incBatchCommon($where, $data) {
        $query = self::where($where);
        foreach($data as $field=>$val) {
            $query = $query->inc($field, $val);
        }
        return $query->update();
    }

	/**
	 * 自减
	 * @param $where
	 * @param $field
	 * @param int $step
	 * @return mixed
	 */
	public static function decCommon($where , $field , $step=1) {
		return self::where($where)->where($field , '>=' , $step)->dec($field , $step)->update();
	}

    /**
     * 自减,多字段
     * @param $where
     * @param $data
     * @return mixed
     */
    public static function decBatchCommon($where, $data) {
        $query = self::where($where);
        foreach($data as $field=>$val) {
            $query = $query->dec($field, $val);
        }
        return $query->update();
    }

    /**
     * 自减.多个字段
     * @param $where
     * @param array $fieldList
     * @return mixed
     */
    public static function decFieldListCommon($where, array $fieldList) {
        $query = self::where($where);
        foreach($fieldList as $field => $step) {
            $query->where($field, '>=', $step)->dec($field, $step);
        }
        return $query->update();
    }

    /**
     * 添加
     * @param $data
     * @return mixed
     */
	public static function addCommon($data) {
		 $obj = self::create($data);
		 return $obj->getKey();
	}

	/**
	 * 批量添加
	 * @param $data
	 * @param int $limit
	 * @return int
	 */
	public static function addBatchCommon($data, $limit=100) {
		return self::limit($limit)->insertAll($data);
	}

	/**
	 * 整理list和count字段
	 * @param $list
	 * @param $count
	 * @return \ListWithCountStruct
	 */
	public static function listWithCountCommon($list, $count) {
		return \ListWithCountStruct::create($list, $count);
	}
}